package com.hspedu.method;

/**
 * @Author Stringzhua
 * @Date 2024/3/15 15:19
 * description:守护线程
 */
public class ThreadMethod03 {
    public static void main(String[] args) throws InterruptedException {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        //如果希望当主线程结束,子线程可以自动结束
        //只需要将子线程设置为守护线程即可
        myDaemonThread.setDaemon(true);
        myDaemonThread.start();
        for (int i = 0; i <=10; i++) {//main线程
            System.out.println("宝强在辛苦的工作......");
            Thread.sleep(1000);
        }
    }
}

class MyDaemonThread extends Thread{
    @Override
    public void run() {
        for(;;){//死循环
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("马蓉和宋喆聊天,哈哈哈......");
        }
    }
}